package org.mch.func.db.tool;

import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.List;

import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.mch.biz.ui.FuncEvent;
import org.mch.comm.exec.MsgException;
import org.mch.swingm.core.MListModel;

import com.mch.main.common.MDebugDLG;

/**
 * 描述：
 * 
 * @author MCH；Date：2008-10-01；<BR/>
 * 
 */
public class DBToolEvent extends FuncEvent {
	private static final long serialVersionUID = 1L;
	private DBHandle dbParse;

	private boolean isOne = true;

	public DBToolEvent() {
		super(null);
	}

	public DBToolEvent(DBToolPanel panel) {
		super(panel);
	}

	protected void onButtonClick(ActionEvent e) throws Exception {
		if (e.getSource() == getPanel().getButton_loadusertable()) {
			loadUserTableList();
		} else if (e.getSource() == getPanel().getBtnhibernatevo()) {
			expHabernateVO();
		} else if (e.getSource() == getPanel().getBtnjavabean()) {
			expJavaBean();
		} else if (e.getSource() == getPanel().getBtnjpabean()) {
			expJPABean();
		} else if (e.getSource() == getPanel().getBtnsql()) {
			expSQL();
		} else if (e.getSource() == getPanel().getBtnbaseeo()) {
			expBaseEO();
		} else if (e.getSource() == getPanel().getBtneasypoi()) {
			expEasyPOIEO();
		} else if (e.getSource() == getPanel().getBtn_Clear()) {
			clearPanel();
		} else if (e.getSource() == getPanel().getTextField()) {
			if (isOne == true) {
				getPanel().getTextField().setText("");
				isOne = false;
			}
		} else if (e.getSource() == getPanel().getBtn_ybj_insertsql()) {
			exp_Ybj_InsertSQL();
		} else if (e.getSource() == getPanel().getBtn_ybj_updatesql()) {
			exp_Ybj_UpdateSQL();
		} else if (e.getSource() == getPanel().getBtn_mybatis_insertsql()) {
			exp_MyBatis_InsertSQL();
		} else if (e.getSource() == getPanel().getBtn_mybatis_updatesql()) {
			exp_MyBatis_UpdateSQL();
		}
	}

	/**
	 * 根据表，生成MyBatis 插入SQL；
	 * 
	 * @throws Exception
	 */
	private void exp_MyBatis_InsertSQL() throws Exception {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}

		String tablename = (String) getPanel().getList_tableName().getSelectedValue();

		boolean isnotpk = getPanel().getIsYbjInsertSqlNotPk().isSelected();
		boolean islower = getPanel().getIsYbjSqlLower().isSelected();
		String ybjsql = dbParse.getInsertMyBatisSQL(tablename, isnotpk, islower);

		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
		getPanel().getTextArea_right().setText(ybjsql);

	}

	/**
	 * 根据表，生成MyBatis 更新SQL；
	 * 
	 * @throws Exception
	 */
	private void exp_MyBatis_UpdateSQL() throws Exception {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}

		String tablename = (String) getPanel().getList_tableName().getSelectedValue();
		boolean islower = getPanel().getIsYbjSqlLower().isSelected();
		String ybjsql = dbParse.getUpdateMyBatisSQL(tablename, islower);

		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
		getPanel().getTextArea_right().setText(ybjsql);

	}

	/**
	 * 根据表，生成预编译 插入SQL；
	 * 
	 * @throws Exception
	 */
	private void exp_Ybj_InsertSQL() throws Exception {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}

		String tablename = (String) getPanel().getList_tableName().getSelectedValue();

		boolean isnotpk = getPanel().getIsYbjInsertSqlNotPk().isSelected();
		boolean islower = getPanel().getIsYbjSqlLower().isSelected();
		String ybjsql = dbParse.getInsertYbjSQL(tablename, isnotpk, islower);

		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
		getPanel().getTextArea_right().setText(ybjsql);

	}

	/**
	 * 根据表，生成预编译 更新SQL；
	 * 
	 * @throws Exception
	 */
	private void exp_Ybj_UpdateSQL() throws Exception {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}

		String tablename = (String) getPanel().getList_tableName().getSelectedValue();
		boolean islower = getPanel().getIsYbjSqlLower().isSelected();
		String ybjsql = dbParse.getUpdateYbjSQL(tablename, islower);

		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
		getPanel().getTextArea_right().setText(ybjsql);

	}

	public void expJPABean() {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}
		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);

		try {
			String expsql = getDbParse().getJPABean((String) getPanel().getList_tableName().getSelectedValue());

			getPanel().getTextArea_right().setText(expsql);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	public void expBaseEO() {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}
		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);

		try {
			String expsql = getDbParse().getBaseEO((String) getPanel().getList_tableName().getSelectedValue());

			getPanel().getTextArea_right().setText(expsql);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	public void expHabernateVO() {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}
		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);

		try {
			String expsql = getDbParse().getHibernateVO((String) getPanel().getList_tableName().getSelectedValue());

			getPanel().getTextArea_right().setText(expsql);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	public void expJavaBean() {
		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);

		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}
		String selName = (String) getPanel().getList_tableName().getSelectedValue();
		if (selName == null || selName.trim().equals("")) {
			throw new MsgException("请选择一个表名！");
		}

		try {
			String expsql = getDbParse().getJavaBean(selName);

			getPanel().getTextArea_right().setText(expsql);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	public void expEasyPOIEO() {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}
		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);

		String selName = (String) getPanel().getList_tableName().getSelectedValue();
		if (selName == null || selName.trim().equals("")) {
			throw new MsgException("请选择一个表名！");
		}

		try {
			String expsql = getDbParse().getEasyPOIEO(selName);

			getPanel().getTextArea_right().setText(expsql);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	public void expSQL() {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}
		StringBuilder insertSql = new StringBuilder();
		List<String> tableNames = getPanel().getList_tableName().getSelectedValuesList();
		if (tableNames != null && tableNames.size() > 0) {
			for (int i = 0, len = tableNames.size(); i < len; i++) {
				try {
					String table = tableNames.get(i);
					String sql = "SELECT * FROM " + table;

					String wheresql = getPanel().getText_wheresql().getText();

					if ((wheresql != null) && (!wheresql.trim().equals(""))) {
						sql = sql + " WHERE " + wheresql;
					}

					String str = getDbParse().getInsertSQL(sql, table, getPanel().getIsDeleteSQL().isSelected(), getPanel().getText_delfield().getText());

					insertSql.append(str);
				} catch (Exception exc) {
					exc.printStackTrace();
				}
			}
		}

		setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL);
		getPanel().getTextArea_right().setText(null);
		getPanel().getTextArea_right().setText(insertSql.toString());

	}

	protected void setSyntaxEditingStyle(String style) {
		String curStyle = getPanel().getTextArea_right().getSyntaxEditingStyle();

		if (!style.equals(curStyle)) {
			getPanel().getTextArea_right().setSyntaxEditingStyle(style);
		}
	}

	public void loadUserTableList() {
		if (getDbParse() == null) {
			throw new MsgException("请先连接数据库！");
		}

		try {
			List<String> list = null;
			String whereShuru = getPanel().getTextField().getText();
			if (whereShuru != null && whereShuru.length() > 0) {
				list = DBInfoUtil.getUserTables(whereShuru);
			} else {
				list = DBInfoUtil.getUserTables();
			}

			((MListModel<String>) getPanel().getList_tableName().getModel()).clear();
			getPanel().getList_tableName().updateUI();

			if ((list == null) || (list.size() <= 0)) {
				return;
			}

			for (int i = 0; i < list.size(); i++) {
				((MListModel<String>) getPanel().getList_tableName().getModel()).addElement(list.get(i));
				getPanel().getList_tableName().updateUI();
			}

		} catch (SQLException exc) {
			MDebugDLG.debug(exc);
		}
	}

	public DBHandle getDbParse() {
		if (dbParse == null) {
			dbParse = new DBHandle();
		}
		return dbParse;
	}

	private void clearPanel() {
		this.getPanel().getTextArea_right().setText("");
	}

	public DBToolPanel getPanel() {
		return (DBToolPanel) super.getPanel();
	}
}